package com.example.service.impl;

import com.example.Repository.TypeRepository;
import com.example.domain.Blog;
import com.example.domain.property.Type;
import com.example.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeRepository typeRepository;
    //新增
    @Override
    public boolean insert(Type type) {
        Type save = typeRepository.save(type);
        if(type.getName().equals(save.getName())){
            return true;
        }
        return false;
    }
    //查询一个
    @Override
    public Type selectOne(Long id) {
        Optional<Type> optional = typeRepository.findById(id);
        Type type=optional.get();
        //Type type = typeRepository.getById(id);
        return type;
    }
    //查询所有分类
    @Override
    public List<Type> findAll() {
        return typeRepository.findAll();
    }

    //分页查询多个
    @Override
    public Page<Type> selectTypePage(int currentPage, int PageSize) {
        Pageable pageable = PageRequest.of(currentPage, PageSize);
        Page<Type> page = typeRepository.findAll(pageable);
        return page;
    }

    //更新
    @Override
    public boolean updateById(Type type) {
        Type save = typeRepository.save(type);
        if(type.getId()==save.getId()){
            return true;
        }
        return false;
    }
    //id删除
    @Override
    public boolean deleteById(Long id) {
        int i = typeRepository.DeteleTypeId(id);  //自定义删除
        return i>0;
    }
}
